import java.util.LinkedList;

/**
 * Created by LI on 2020/5/31.
 */
public class Ysf {
    public static void main(String[] args) {
        int n = 10, m = 11;
        lastRemain(n, m);
    }

    public static void lastRemain(int n, int m) {
        LinkedList<Integer> queueList = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            queueList.add(i);
        }
        while (queueList.size() >= 2) {
            int temp = queueList.size() / m;

            if (temp == 0) {
                while (queueList.size() > 1) {
                    int killIndex = m % queueList.size();
                    //删除在最后一个位置
                    if (killIndex == 0) {
                        queueList.pollLast();
                        continue;
                    }
                    //删除在其它位置
                    killIndex--;
                    queueList.remove(killIndex);
                    for (int i = 0; i < killIndex; i++) {
                        queueList.addLast(queueList.pollFirst());
                    }
                }
                continue;
            }

            for (int i = 1; i <= temp; i++) {
                queueList.remove((m * i) - i);
            }
            /**
             * 最后删除位置的前一个位置
             */
            int lastRemovePrePosition = temp * (m - 1) - 1;
            for (int i = queueList.size() - 1; i > lastRemovePrePosition; i--) {
                queueList.addFirst(queueList.pollLast());
            }
            System.out.println("list" + queueList.toString());
        }
        System.out.println(queueList.toString());
    }
}
